package com.lovo.dao.impl;

import com.lovo.bean.User;
import com.lovo.dao.BaseDAO;
import com.lovo.dao.UserDAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public User findByNameAndPwd(String name, String pwd) {
        PreparedStatement statement = null;
        ResultSet result = null;
        User user = null;
        try {
            String sql = "select * from t_user where u_name=? and u_pwd=?";
            statement = con.prepareStatement(sql);
            statement.setString(1,name);
            statement.setString(2,pwd);
            result = statement.executeQuery();
            if(result.next()){
                user = new User();
                user.setId(result.getInt("u_id"));
                user.setName(result.getString("u_name"));
                user.setPwd(result.getString("u_pwd"));
                user.setRealName(result.getString("u_real_name"));
                user.setLevel(result.getString("u_level"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(con,statement,result);
        }
        return user;
    }

    @Override
    public List find() {
        return null;
    }

    @Override
    public int insert(User user) {
        return 0;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public int delete(int id) {
        return 0;
    }

    public static void main(String[] args) {
        UserDAO userDAO = new UserDAOImpl();
        System.out.println(userDAO.findByNameAndPwd("admin","123456"));
    }
}
